package com.gitee.wsl.time

import com.gitee.wsl.ext.base.currentTimeMillis
import kotlin.time.Instant
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import kotlin.jvm.JvmInline
import kotlin.time.ExperimentalTime

/**
 * Timestamp
 *
 * @constructor Create empty Timestamp
 * @property value
 */
@JvmInline
value class Timestamp(val value: Long) : Comparable<Timestamp> {
    override fun compareTo(other: Timestamp): Int = value.compareTo(other.value)

    /** Add two [Timestamp]s together. */
     operator fun plus(other: Timestamp) =
        Timestamp(value = this.value + other.value)

    /** Subtract a Timestamp from another one. */
     operator fun minus(other: Timestamp) =
        Timestamp(value = this.value - other.value)

    inline val asTimestampSeconds: Long get() = value / 1000

    inline val asTimestampMilliseconds: Long get() = value

    companion object {
        /**
         * Create a Timestamp
         *
         * @return
         */
        fun now(): Timestamp = Timestamp(currentTimeMillis)
    }
}

 @OptIn(ExperimentalTime::class)
 fun Timestamp.toInstant(timeZone: TimeZone = TimeZone.currentSystemDefault()): Instant =
    Instant.fromEpochMilliseconds(this.value)

 @OptIn(ExperimentalTime::class)
 fun Timestamp.toLocalDateTime(timeZone: TimeZone = TimeZone.currentSystemDefault()): LocalDateTime =
    Instant.fromEpochMilliseconds(this.value).toLocalDateTime(timeZone)